package userInterface;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Auction;
import model.Control;
import model.Item;

/**
 * @author fahad Muzaffar
 * @author Daniel Bartholomew
 */
@SuppressWarnings("serial")
public class ItemForm extends JFrame {
		
	private Control my_control;
	
	private Auction my_auction;

	private JButton btnBack = null;
	
	/**
	 * The auction text field is use in the button to check if auction text field is empty or not.
	 * When we submit the form it is important to have all the information before submitting. 
	 */
	private JTextField item_name_textfield;

	/**
	 * The contact text field is use in the button to check if contact text field is empty or not.
	 * When we submit the form it is important to have all the information before submitting. 
	 */
	private JTextField minimum_starting_bid;
	
	private JTextField item_notes_textfield;
	
	/**
	 * Constructor to display a GUI for auction forms. Constructor
	 */
	public ItemForm(Control the_control, Auction the_auction) {
		super("Item Form");
		my_control = the_control;
		my_auction = the_auction;
		this.setSize(640, 480);
		this.setVisible(true);
		this.setResizable(false);	
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		item();
	}
	public void item()
	{
		setLayout(new GridLayout(15, 1));
		
		JPanel item_name = new JPanel();
		JLabel item_name_display = new JLabel("Item Name-----------------:");
		item_name.add(item_name_display);
		item_name_textfield = new JTextField(30);
		item_name.add(item_name_textfield);
		add(item_name);
		
		JPanel min_starting_bid = new JPanel();
		JLabel minimum_starting_bid_display = new JLabel("Minimum starting bid-------------:");
		min_starting_bid.add(minimum_starting_bid_display);
		minimum_starting_bid = new JTextField(30);
		min_starting_bid.add(minimum_starting_bid);
		add(min_starting_bid);
		
		JPanel item_notes = new JPanel();
		JLabel item_notes_display = new JLabel("Item Notes----------------:");
		item_notes.add(item_notes_display);
		item_notes_textfield = new JTextField(30);
		item_notes.add(item_notes_textfield);
		add(item_notes);

		JButton submit = new JButton("Submit");
		add(submit);
		add(getBtnBack());

		submit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
	
				if (item_name_textfield.getText().length() == 0 ||
						minimum_starting_bid.getText().length() == 0) {
					JOptionPane.showMessageDialog(null,"Form is incomplete");
					return;
				}
				String name = item_name_textfield.getText();
				double minbid = -1;
				try {
					minbid = Double.parseDouble(minimum_starting_bid.getText());
					if (minbid <= 0) {
						JOptionPane.showMessageDialog(null,	"Bad Minimum Bid");
						return;
					}
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null,	"Bad Minimum Bid");
					return;
				}
				String notes = null;
				if (item_notes_textfield.getText().length() != 0) {
					notes = item_notes_textfield.getText();
				}
				Item newItem = new Item(name, minbid, notes);
				my_auction.addItem(newItem);
				my_control.saveItems(my_auction);
				new ItemListView(my_control, my_auction);
				dispose();
			}});
	}
	

	private JButton getBtnBack() {
		if (btnBack == null) {
			btnBack = new JButton("Cancel");
			//btnBack.setBounds(new Rectangle(495, 404, 106, 31));
			btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ItemListView ilv = new ItemListView(my_control, my_auction);
					
					ilv.setLocationRelativeTo(null);
					ilv.setVisible(true);
					dispose();
				}
			});
		}
		return btnBack;
	}
}